package com.rjkj.medicalwaste.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import com.rjkj.medicalwaste.bean.HandoverInfo
import com.rjkj.medicalwaste.bean.HandoverInfoBean
import com.rjkj.medicalwaste.databinding.ItemOutboundHisChildListBinding
import com.rjkj.medicalwaste.databinding.ItemOutboundHisGroupListBinding
import java.math.BigDecimal


class OutboundHistoryListAdapter(var list: List<HandoverInfoBean>, val context: Context) :
    BaseExpandableListAdapter() {

    override fun getGroupCount(): Int {
        return list.size
    }

    override fun getChildrenCount(groupPosition: Int): Int {
        return list[groupPosition].handoverInfo.size
    }

    override fun getGroup(groupPosition: Int): Any {
        return list[groupPosition]
    }

    override fun getChild(groupPosition: Int, childPosition: Int): Any {
        return list[groupPosition].handoverInfo[childPosition]
    }

    override fun getGroupId(groupPosition: Int): Long {
        return groupPosition.toLong()
    }

    override fun getChildId(groupPosition: Int, childPosition: Int): Long {
        return childPosition.toLong()
    }

    override fun hasStableIds(): Boolean {
        return true
    }

    override fun getGroupView(
        groupPosition: Int,
        isExpanded: Boolean,
        convertView: View?,
        parent: ViewGroup?
    ): View {
        var mConvertView: View? = convertView
        if (mConvertView == null) {
            val binding = ItemOutboundHisGroupListBinding.inflate(
                LayoutInflater.from(parent?.context),
                parent,
                false
            )
            mConvertView = binding.root
            val outboundHisGroupHolder = OutboundHisGroupHolder(binding)
            outboundHisGroupHolder.bind(list[groupPosition], isExpanded)
            mConvertView.tag = outboundHisGroupHolder
        } else {
            val outboundHisGroupHolder = mConvertView.tag as OutboundHisGroupHolder
            outboundHisGroupHolder.bind(list[groupPosition], isExpanded)
        }

        return mConvertView
    }

    override fun getChildView(
        groupPosition: Int,
        childPosition: Int,
        isLastChild: Boolean,
        convertView: View?,
        parent: ViewGroup?
    ): View {
        var mConvertView = convertView
        if (mConvertView == null) {
            val binding = ItemOutboundHisChildListBinding.inflate(
                LayoutInflater.from(parent?.context),
                parent,
                false
            )
            mConvertView = binding.root
            val outboundHisChildHolder = OutboundHisChildHolder(binding)
            outboundHisChildHolder.bind(list[groupPosition].handoverInfo[childPosition])
            mConvertView.tag = outboundHisChildHolder
        } else {
            val outboundHisChildHolder = mConvertView.tag as OutboundHisChildHolder
            outboundHisChildHolder.bind(list[groupPosition].handoverInfo[childPosition])
        }

        return mConvertView
    }

    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
        return true
    }

    inner class OutboundHisGroupHolder(private val binding: ItemOutboundHisGroupListBinding) {
        fun bind(item: HandoverInfoBean, isExpanded: Boolean) {
            binding.bean = item
            val count = item.handoverInfo.sumBy { it.boxCount }.toString()
            val sumByDouble = item.handoverInfo.sumOf {
                if (it.weight.isNotEmpty()) {
                    BigDecimal(it.weight)
                } else BigDecimal.ZERO
            }.setScale(2, BigDecimal.ROUND_HALF_UP).toString()
            binding.weight = sumByDouble
            binding.count = count
            binding.isExpanded = isExpanded
            binding.executePendingBindings()
        }
    }


    inner class OutboundHisChildHolder(private val binding: ItemOutboundHisChildListBinding) {
        fun bind(item: HandoverInfo) {
            binding.bean = item
            binding.executePendingBindings()
        }
    }

}